package com.baizhi.service.impl;

import com.baizhi.dao.CmfzAlbumDao;
import com.baizhi.dao.CmfzAudioDao;
import com.baizhi.entity.CmfzAlbum;
import com.baizhi.entity.CmfzAudio;
import com.baizhi.service.CmfzAudioService;
import com.baizhi.util.FileUpload;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/4/10 0010.
 */
@Service
public class CmfzAudioServiceImpl implements CmfzAudioService {
    @Autowired
    private CmfzAudioDao cmfzAudioDao;
    @Autowired
    private CmfzAlbumDao cmfzAlbumDao;
    
    
    @Override
    public List<CmfzAlbum> showAll() {
        List<CmfzAlbum> cmfzAlbums = cmfzAlbumDao.selectList(null);
       for (CmfzAlbum cmfzAlbum:cmfzAlbums){
           QueryWrapper<CmfzAudio> wrapper=new QueryWrapper<>();
           wrapper.eq("album_id",cmfzAlbum.getAlbumId()).orderByAsc("audio_order");
           List<CmfzAudio> cmfzAudios = cmfzAudioDao.selectList(wrapper);
           cmfzAlbum.setCmfzAudios(cmfzAudios);
       }
        return cmfzAlbums;
    }

    @Override
    public Map add(MultipartFile audio, CmfzAudio cmfzAudio, HttpServletRequest request) {
        Map map = new HashMap();
        try {
            FileUpload.uploadAudio(audio,cmfzAudio,request);
            cmfzAudioDao.insert(cmfzAudio);
            map.put("flag", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag", false);
        }
        return map;
    }
}
